package top.yeyusmile.utils;


import okhttp3.*;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2021/1/15 14:22
 */
public class HttpUtil {




    /**
     * get 异步
     *
     * @param address
     * @param callback
     */
    public static void httpGet(String address, String cookie, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        client.dispatcher().setMaxRequests(3000);
        client.dispatcher().setMaxRequestsPerHost(1000);
        Request request = new Request.Builder()
                .url(address)
                .addHeader("cookie", cookie)
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * post 异步
     *
     * @param address
     * @param requestBody
     * @param callback
     */
    public static void httpPost(String address, RequestBody requestBody, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        client.dispatcher().setMaxRequests(3000);
        client.dispatcher().setMaxRequestsPerHost(1000);
        Request request = new Request.Builder()
                .url(address)
                .post(requestBody)
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * https get 异步
     *
     * @param address
     * @param cookie
     * @param callback
     */
    public static void httpsGet(String address, String cookie, Callback callback) {


        final OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                .sslSocketFactory(SSLUtils.getSSLSocketFactory(), SSLUtils.getX509TrustManager())
                .hostnameVerifier(SSLUtils.getHostnameVerifier())
                .build();

        // OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(address)
                .addHeader("cookie", cookie)
//                .addHeader("content-type","charset=gb2312")//content-type: text/html; charset=UTF-8
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .build();
        client.newCall(request).enqueue(callback);
    }


    /**
     * https post 异步
     *
     * @param address
     * @param requestBody
     * @param callback
     */
    public static void httpsPost(String address, RequestBody requestBody, String cookie, Callback callback) {
        final OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                .sslSocketFactory(SSLUtils.getSSLSocketFactory(), SSLUtils.getX509TrustManager())
                .hostnameVerifier(SSLUtils.getHostnameVerifier())
                .build();

        // OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(address)
                .post(requestBody)
                .addHeader("cookie", cookie)
//                .addHeader("content-type","charset=gb2312")//content-type: text/html; charset=UTF-8
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .build();
        client.newCall(request).enqueue(callback);


    }


    /**
     * http get
     * 同步方法
     *
     * @param address
     * @return
     */
    public static synchronized String synHttpGet(String address, String cookie) {

        OkHttpClient client = new OkHttpClient();
        client.dispatcher().setMaxRequests(3000);
        client.dispatcher().setMaxRequestsPerHost(1000);
        Request request = new Request.Builder()
                .url(address)
                .addHeader("cookie", cookie)
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .build();
        String ret = "";
        try {
            ret =  client.newCall(request).execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ret;


    }


    /**
     * http post
     * 同步方法
     *
     * @param address
     * @param requestBody
     * @return
     */
    public static synchronized String synHttpPost(String address, RequestBody requestBody) {

        OkHttpClient client = new OkHttpClient();
        client.dispatcher().setMaxRequests(3000);
        client.dispatcher().setMaxRequestsPerHost(1000);
        Request request = new Request.Builder()
                .url(address)
                .post(requestBody)
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .build();

        String ret = "";
        try {
            ret =  client.newCall(request).execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * https get
     *
     * @param address
     * @return
     */
//    public static synchronized String synHttpsGet(String address, String cookie) {
//        final String[] ret = {null};
//        doHttpsGetComplted = false;
//        HttpUtil.httpsGet(address, cookie, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                doHttpsGetComplted = true;
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                ret[0] = response.body().string();
//                response.close();
//                doHttpsGetComplted = true;
//
//            }
//        });
//        while (!doHttpsGetComplted) {
//        }
//        return ret[0];
//    }


    /**
     * https post
     * 同步
     *
     * @param address
     * @return
     */
//    public static synchronized String synHttpsPost(String address, RequestBody requestBody, String cookie) {
//        final String[] ret = {null};
//        doHttpsPostComplted = false;
//        HttpUtil.httpsPost(address, requestBody, cookie, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                doHttpsPostComplted = true;
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                ret[0] = response.body().string();
//                response.close();
//                doHttpsPostComplted = true;
//
//            }
//        });
//        while (!doHttpsPostComplted) {
//        }
//        return ret[0];
//    }


    /**
     * @param js
     * @param funName
     */
    public static Object execJS(String js, String funName) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");
        try {
            engine.eval(js);
            if (engine instanceof Invocable) {
                Invocable in = (Invocable) engine;
                return in.invokeFunction(funName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




}
